package frontier;


import java.util.*;
import util.MyXmlUtil;

public class LinkQueue {
	//private  Set<String> visitedUrl = new HashSet<String>();
	//private  BloomFilter visitedUrl = new  BloomFilter();//用bloomfilter来构造已访问队列
	//private  List<String> unVisitedUrl = new  LinkedList<String>();
	//private RedisQueue unVisitedUrl = new RedisQueue("visitedUrl");
	
	private VisitedUrl visitedUrl;
	private UnVisitedUrl unVisitedUrl;
	public LinkQueue() throws Exception {
		MyXmlUtil configure;
		
		configure = new MyXmlUtil("configure.xml");
		String useRedis = configure.getConfigure("redis", "use");
		
		String useBloomFilter = configure.getConfigure("bloomFilter", "use");
		
		if(useRedis.equalsIgnoreCase("yes")) {
			//是否使用redis数据库,构造未访问队列
			 unVisitedUrl = new RedisQueue("visitedUrl");
		}else {
			 unVisitedUrl = new NUQueue();
		}
		
		if(useBloomFilter.equalsIgnoreCase("yes")) {
			//是否使用bloomfilter,构造已访问队列 
			visitedUrl = new BloomFilter();
		}else {
			visitedUrl = new NVQueue();
		}
	}
	
	//添加访问过的URL,要同步
	public  synchronized void addVisitedUrl (String url)
	{
		visitedUrl.add(url);
	}
	
	public long getUnvisitedUrlSize()
	{
		return unVisitedUrl.size();
	}
	
	
	
	
	//获取未访问url，并出队 要同步
	public   synchronized String unVisitedUrlDeQueue()
	{
		if(!unVisitedUrl.isEmpty())
			
			return unVisitedUrl.remove(0);
		else
			return null;
	}
	//添加示访问url 要同步 
	public   synchronized void addUnvisitedUrl(String url)
	{
		if(url != null && !url.trim().equals("")&&!visitedUrl.contains(url)
			&& !unVisitedUrl.contains(url) )
		{
			unVisitedUrl.add(url);
		
		}
	}
	
	
	public  boolean unVisitedUrlsEmpty()
	{
		return unVisitedUrl.isEmpty();
	}
}
